<template>
  <aside class="big-left" :class="bigLeftClasses">
    <!-- Header -->
    <div class="big-left__header">
      <div class="user-info">
        <img
          :src="userAvatar"
          :alt="userName"
          class="user-avatar"
        />
        <div class="user-details">
          <h3 class="user-name">{{ userName }}</h3>
          <span class="user-status" :class="`user-status--${userStatus}`">
            {{ statusText }}
          </span>
        </div>
      </div>
      
      <div class="header-actions">
        <button
          @click="toggleTheme"
          class="action-button"
          :title="ui.theme === 'light' ? '切换到深色模式' : '切换到浅色模式'"
        >
          <span class="icon">{{ ui.theme === 'light' ? '🌙' : '☀️' }}</span>
        </button>
        
        <button
          @click="openSettings"
          class="action-button"
          title="设置"
        >
          <span class="icon">⚙️</span>
        </button>
      </div>
    </div>
    
    <!-- Navigation -->
    <nav class="big-left__navigation">
      <ul class="nav-list">
        <li
          v-for="item in navigation"
          :key="item.id"
          class="nav-item"
        >
          <button
            @click="handleNavigate(item.id)"
            :class="[
              'nav-button',
              { 'nav-button--active': activeItem === item.id }
            ]"
          >
            <span class="nav-icon">{{ getIcon(item.icon) }}</span>
            <span class="nav-label">{{ item.label }}</span>
            <span
              v-if="getNotificationCount(item.id) > 0"
              class="nav-badge"
            >
              {{ getNotificationCount(item.id) }}
            </span>
          </button>
        </li>
      </ul>
    </nav>
    
    <!-- Quick Actions -->
    <div class="big-left__quick-actions">
      <button
        @click="createNewChat"
        class="quick-action-button quick-action-button--primary"
        title="新建聊天"
      >
        <span class="icon">💬</span>
        <span class="label">新建聊天</span>
      </button>
      
      <button
        @click="openSearch"
        class="quick-action-button"
        title="搜索 (Ctrl+K)"
      >
        <span class="icon">🔍</span>
        <span class="label">搜索</span>
      </button>
    </div>
    
    <!-- Status Indicator -->
    <div class="big-left__status">
      <div class="connection-status" :class="connectionStatusClass">
        <span class="status-dot"></span>
        <span class="status-text">{{ connectionStatusText }}</span>
      </div>
    </div>
    
    <!-- Footer -->
    <div class="big-left__footer">
      <div class="app-info">
        <span class="app-name">聊天系统</span>
        <span class="app-version">v1.0.0</span>
      </div>
      
      <button
        @click="handleLogout"
        class="logout-button"
        title="退出登录"
      >
        <span class="icon">🚪</span>
      </button>
    </div>
  </aside>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useStores } from '@/composables'
import type { NavigationItem } from '@/types'

// Props
interface Props {
  navigation: NavigationItem[]
  activeItem: string
}

defineProps<Props>()

// Emits
const emit = defineEmits<{
  navigate: [itemId: string]
}>()

// Stores
const { userStore: user, chatStore: chat, uiStore: ui } = useStores()

// Computed properties
const bigLeftClasses = computed(() => ({
  'big-left--collapsed': ui.sidebarCollapsed,
  'big-left--mobile': ui.isMobile,
  'big-left--dark': ui.theme === 'dark'
}))

const userName = computed(() => user.userName || '未登录')
const userAvatar = computed(() => user.userAvatar)
const userStatus = computed(() => user.userStatus)

const statusText = computed(() => {
  switch (user.userStatus) {
    case 'online': return '在线'
    case 'away': return '离开'
    case 'offline': return '离线'
    default: return '未知'
  }
})

const connectionStatusClass = computed(() => ({
  'connection-status--connected': true, // TODO: Add real connection status
  'connection-status--disconnected': false,
  'connection-status--connecting': false
}))

const connectionStatusText = computed(() => {
  // TODO: Add real connection status logic
  return '已连接'
})

// Methods
const handleNavigate = (itemId: string) => {
  emit('navigate', itemId)
}

const getIcon = (iconName: string): string => {
  const icons: Record<string, string> = {
    chat: '💬',
    contacts: '👥',
    settings: '⚙️',
    notifications: '🔔',
    help: '❓'
  }
  return icons[iconName] || '📄'
}

const getNotificationCount = (itemId: string): number => {
  switch (itemId) {
    case 'chats':
      return chat.unreadChatsCount
    case 'contacts':
      return 0 // TODO: Add contact notifications
    case 'notifications':
      return ui.unreadNotificationsCount
    default:
      return 0
  }
}

const toggleTheme = () => {
  ui.toggleTheme()
}

const openSettings = () => {
  ui.openModal('settings')
  emit('navigate', 'settings')
}

const createNewChat = () => {
  ui.openModal('createChat')
}

const openSearch = () => {
  ui.openModal('search')
}

const handleLogout = async () => {
  try {
    await user.logout()
    ui.addNotification({
      type: 'success',
      title: '退出成功',
      message: '您已成功退出登录'
    })
  } catch (error) {
    ui.addNotification({
      type: 'error',
      title: '退出失败',
      message: error instanceof Error ? error.message : '退出登录时发生错误'
    })
  }
}
</script>

<style scoped>
.big-left {
  width: var(--sidebar-width);
  height: 100vh;
  background: linear-gradient(180deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
  color: var(--color-text-inverse);
  display: flex;
  flex-direction: column;
  position: relative;
  box-shadow: var(--shadow-lg);
  z-index: var(--z-fixed);
}

.big-left--dark {
  background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
}

.big-left--mobile {
  position: fixed;
  top: 0;
  left: 0;
  z-index: var(--z-modal);
  transform: translateX(0);
  transition: transform var(--transition-normal);
}

.big-left--collapsed.big-left--mobile {
  transform: translateX(-100%);
}

/* Header */
.big-left__header {
  padding: var(--spacing-xl) var(--spacing-lg);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.user-info {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  margin-bottom: var(--spacing-lg);
}

.user-avatar {
  width: 48px;
  height: 48px;
  border-radius: var(--border-radius-full);
  border: 2px solid rgba(255, 255, 255, 0.2);
  object-fit: cover;
}

.user-details {
  flex: 1;
  min-width: 0;
}

.user-name {
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-semibold);
  margin: 0 0 var(--spacing-xs) 0;
  color: var(--color-text-inverse);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-status {
  font-size: var(--font-size-xs);
  opacity: 0.8;
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
}

.user-status::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: var(--border-radius-full);
  background-color: currentColor;
}

.user-status--online::before {
  background-color: var(--color-success);
}

.user-status--away::before {
  background-color: var(--color-warning);
}

.user-status--offline::before {
  background-color: var(--color-secondary);
}

.header-actions {
  display: flex;
  gap: var(--spacing-sm);
}

.action-button {
  width: 36px;
  height: 36px;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  color: var(--color-text-inverse);
  border-radius: var(--border-radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition-fast);
}

.action-button:hover {
  background: rgba(255, 255, 255, 0.2);
}

.action-button .icon {
  font-size: var(--font-size-base);
}

/* Navigation */
.big-left__navigation {
  flex: 1;
  padding: var(--spacing-lg) 0;
  overflow-y: auto;
}

.nav-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-item {
  margin-bottom: var(--spacing-xs);
}

.nav-button {
  width: 100%;
  padding: var(--spacing-md) var(--spacing-lg);
  border: none;
  background: none;
  color: var(--color-text-inverse);
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  font-size: var(--font-size-sm);
  transition: background-color var(--transition-fast);
  position: relative;
}

.nav-button:hover {
  background: rgba(255, 255, 255, 0.1);
}

.nav-button--active {
  background: rgba(255, 255, 255, 0.15);
  font-weight: var(--font-weight-medium);
}

.nav-button--active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--color-text-inverse);
}

.nav-icon {
  font-size: var(--font-size-lg);
  width: 24px;
  text-align: center;
}

.nav-label {
  flex: 1;
}

.nav-badge {
  background: var(--color-error);
  color: white;
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-bold);
  padding: 2px 6px;
  border-radius: var(--border-radius-full);
  min-width: 18px;
  text-align: center;
}

/* Quick Actions */
.big-left__quick-actions {
  padding: var(--spacing-lg);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
}

.quick-action-button {
  width: 100%;
  padding: var(--spacing-md);
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.1);
  color: var(--color-text-inverse);
  border-radius: var(--border-radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  font-size: var(--font-size-sm);
  transition: all var(--transition-fast);
}

.quick-action-button:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.quick-action-button--primary {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.3);
  font-weight: var(--font-weight-medium);
}

.quick-action-button .icon {
  font-size: var(--font-size-base);
}

.quick-action-button .label {
  flex: 1;
}

/* Status */
.big-left__status {
  padding: var(--spacing-md) var(--spacing-lg);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.connection-status {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  font-size: var(--font-size-xs);
  opacity: 0.8;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--border-radius-full);
  background: var(--color-success);
  animation: pulse 2s infinite;
}

.connection-status--disconnected .status-dot {
  background: var(--color-error);
}

.connection-status--connecting .status-dot {
  background: var(--color-warning);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* Footer */
.big-left__footer {
  padding: var(--spacing-lg);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-info {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xs);
}

.app-name {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  opacity: 0.9;
}

.app-version {
  font-size: var(--font-size-xs);
  opacity: 0.6;
}

.logout-button {
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  color: var(--color-text-inverse);
  border-radius: var(--border-radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition-fast);
}

.logout-button:hover {
  background: rgba(255, 255, 255, 0.2);
}

.logout-button .icon {
  font-size: var(--font-size-sm);
}

/* Responsive */
@media (max-width: 768px) {
  .big-left {
    width: 280px;
  }
}

/* Scrollbar */
.big-left__navigation::-webkit-scrollbar {
  width: 4px;
}

.big-left__navigation::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
}

.big-left__navigation::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: var(--border-radius-full);
}

.big-left__navigation::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.5);
}
</style>